home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / guis / iedit / developer / loaders / gtb / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-23  |  4.0 KB  |  161 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Loaders/defs.h"
  15. #include <gadtoolsbox/gtxbase.h>
  16.  
  17. extern struct Library *LibInit   ( __A0 BPTR );
  18. extern struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  19. extern long            LibClose  ( __A0 struct Library * );
  20. extern long            LibExpunge( __A0 struct Library * );
  21.  
  22. struct Library *LibBase = NULL;
  23.  
  24. long SysBase            = NULL;
  25. long DOSBase            = NULL;
  26. struct GTXBase *GTXBase = NULL;
  27. long NoFragBase         = NULL;
  28. long UtilityBase        = NULL;
  29. BPTR SegList            = NULL;
  30.  
  31. /*
  32.  *    The Initialization routine is given only a seglist pointer.  Since
  33.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  34.  *    and return either NULL or the library pointer.  Exec has Forbid()
  35.  *    for us during the call.
  36.  */
  37.  
  38. struct Library *LibInit( __A0 BPTR segment )
  39. {
  40.  
  41.     struct Library *lib = NULL;
  42.  
  43.     static const long Vectors[] = {
  44.  
  45.     (long)ALibOpen,
  46.     (long)ALibClose,
  47.     (long)ALibExpunge,
  48.     (long)ALibReserved,
  49.  
  50.     (long)LoadGUI,
  51.     (long)LoadWindows,
  52.     (long)LoadGadgets,
  53.     (long)LoadScreen,
  54.     -1
  55.     };
  56.  
  57.     SysBase = *(long *)4;
  58.  
  59.     if (DOSBase = OpenLibrary("dos.library", 0)) {
  60.  
  61.     if(GTXBase = OpenLibrary("gadtoolsbox.library", 39)) {
  62.  
  63.         NoFragBase  = GTXBase->NoFragBase;
  64.         UtilityBase = GTXBase->UtilityBase;
  65.  
  66.         if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct Library), NULL)) {
  67.  
  68.         lib->lib_Node.ln_Type = NT_LIBRARY;
  69.         lib->lib_Node.ln_Name = LibName;
  70.         lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  71.         lib->lib_Version      = 37;
  72.         lib->lib_Revision     = 0;
  73.         lib->lib_IdString     = (APTR)LibId;
  74.  
  75.         SegList = segment;
  76.  
  77.         AddLibrary(lib);
  78.         }
  79.     }
  80.     }
  81.  
  82.     return( lib );
  83. }
  84.  
  85. /*
  86.  *    Open is given the library pointer and the version request.  Either
  87.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  88.  *    Exec has Forbid() for us during the call.
  89.  */
  90.  
  91. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  92. {
  93.     ++lib->lib_OpenCnt;
  94.  
  95.     lib->lib_Flags &= ~LIBF_DELEXP;
  96.  
  97.     return(lib);
  98. }
  99.  
  100. /*
  101.  *    Close is given the library pointer and the version request.  Be sure
  102.  *    not to decrement the open count if already zero.  If the open count
  103.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  104.  *    and return the seglist.  Otherwise we return NULL.
  105.  *
  106.  *    Note that this routine never sets LIBF_DELEXP on its own.
  107.  *
  108.  *    Exec has Forbid() for us during the call.
  109.  */
  110.  
  111. long LibClose( __A0 struct Library *lib )
  112. {
  113.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  114.     return(NULL);
  115.  
  116.     if (lib->lib_Flags & LIBF_DELEXP)
  117.     return(LibExpunge(lib));
  118.  
  119.     return(NULL);
  120. }
  121.  
  122. /*
  123.  *    We expunge the library and return the Seglist ONLY if the open count
  124.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  125.  *    flag and return NULL.
  126.  *
  127.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  128.  *    might be called from the memory allocator and thus we CANNOT DO A
  129.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  130.  *
  131.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  132.  *    therefore freeze if we called it ourselves.  As far as I can tell
  133.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  134.  *    below.
  135.  */
  136.  
  137. long LibExpunge( __A0 struct Library *lib )
  138. {
  139.     if (lib->lib_OpenCnt) {
  140.  
  141.     lib->lib_Flags |= LIBF_DELEXP;
  142.     return(NULL);
  143.     }
  144.  
  145.     Remove(&lib->lib_Node);
  146.  
  147.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  148.  
  149.     if( DOSBase ) {
  150.     CloseLibrary((struct Library *)DOSBase);
  151.     DOSBase = NULL;
  152.     }
  153.  
  154.     if( GTXBase ) {
  155.     CloseLibrary((struct Library *)GTXBase );
  156.     GTXBase = NULL;
  157.     }
  158.  
  159.     return((long)SegList);
  160. }
  161.